package top.codetheory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//定义一个Java类TreeNode，用来表示树的节点
class TreeNode {
  int id; //节点的id
  Integer pid; //节点的父节点的id，如果为null，说明是顶层节点
  int value; //节点的值
  List<TreeNode> children; //节点的子节点列表

  //构造函数，根据id, pid, value初始化节点
  public TreeNode(int id, Integer pid, int value) {
    this.id = id;
    this.pid = pid;
    this.value = value;
    this.children = new ArrayList<>(); //初始化空的子节点列表
  }

  //定义一个Java函数convert，接受一个TreeNode的列表作为参数，返回一个TreeNode的列表作为结果
  public List<TreeNode> convert(List<TreeNode> nodes) {
    List<TreeNode> res = new ArrayList<>(); //创建空的结果列表
    for (TreeNode node : nodes) { //遍历参数列表中的每个节点
      if (node.pid == null) { //判断节点的pid是否为null，如果是，说明是一个顶层节点
        res.add(node); //将节点加入到结果列表中
        List<TreeNode> others = new ArrayList<>(nodes); //创建一个新的列表，存储除了当前节点之外的其他节点
        others.remove(node); //从新的列表中移除当前节点
        List<TreeNode> children = convert(others); //调用convert函数，传入新的列表作为参数，得到返回值children
        node.children = children; //将children赋值给当前节点的children属性
      }
    }
    return res; //返回结果列表
  }

  public static void main(String[] args) {
    String json = "[{\"id\":1,\"pid\":null,\"value\":1},{\"id\":1,\"pid\":2,\"value\":2},{\"id\":3,\"pid\":null,\"value\":3}]";;
    // 将json字符串转换为List<Map<String, Object>>对象
//    Map<String, Object> node1 = new HashMap<>();
//    node1.put("id", 1);
//    node1.put("pid", null);
//    node1.put("value", 1);
//    nodes.add(node1);
//    Map<String, Object> node2 = new HashMap<>();
//    node2.put("id", 2);
//    node2.put("pid", 1);
//    node2.put("value", 2);
//    nodes.add(node2);
//    Map<String, Object> node3 = new HashMap<>();
//    node3.put("id", 3);
//    node3.put("pid", null);
//    node3.put("value", 3);
//    nodes.add(node3);
    List<Map<String, Object>> nodes = parseJson(json);
    List<Map<String, Object>> tree = buildTree(nodes);
    System.out.println(tree);
  }

  // 将请求参数改为json字符串
  public static List<Map<String, Object>> buildTree(List<Map<String, Object>> nodes) {
    Map<Object, List<Map<String, Object>>> groups = new HashMap<>();
    for (Map<String, Object> node : nodes) {
      Object pid = node.get("pid");
      if (pid == null) {
        pid = "null";
      }
      if (!groups.containsKey(pid)) {
        groups.put(pid, new ArrayList<>());
      }
      groups.get(pid).add(node);
    }
    return buildSubtree(groups, "null");
  }

  private static List<Map<String, Object>> buildSubtree(Map<Object, List<Map<String, Object>>> groups, Object pid) {
    List<Map<String, Object>> children = groups.get(pid);
    if (children == null) {
      return new ArrayList<>();
    }
    List<Map<String, Object>> subtree = new ArrayList<>();
    for (Map<String, Object> child : children) {
      child.put("children", buildSubtree(groups, child.get("id")));
      subtree.add(child);
    }
    return subtree;
  }
  public static List<Map<String, Object>> parseJson(String json) {
    List<Map<String, Object>> result = new ArrayList<>();
    String[] tokens = json.split("[{},]");
    Map<String, Object> current = null;
    for (String token : tokens) {
      if (token.isEmpty()) {
        continue;
      }
      if (token.startsWith("\"id\":")) {
        if (current != null) {
          result.add(current);
        }
        current = new HashMap<>();
        current.put("id", Integer.parseInt(token.substring(5)));
      } else if (token.startsWith("\"pid\":")) {
        current.put("pid", token.substring(6).equals("null") ? null : Integer.parseInt(token.substring(6)));
      } else if (token.startsWith("\"value\":")) {
        current.put("value", Integer.parseInt(token.substring(8)));
      }
    }
    if (current != null) {
      result.add(current);
    }
    return result;
  }
}

